package org.hawk.gwt.ppc.parser.impl;

import java.io.InputStream;
import java.util.List;

import junit.framework.TestCase;

import org.hawk.gwt.ppc.parser.FieldDescriptor;
import org.hawk.gwt.ppc.parser.JavaParser;
import org.hawk.gwt.ppc.parser.impl.mock.ClassMock1;
import org.hawk.gwt.ppc.utils.Name;
import org.hawk.gwt.ppc.utils.PreprocessorException;

public class FieldsDescriptorTestCase extends TestCase {
	
	public void testRawFields() throws PreprocessorException {
		FieldDescriptor fld = getFields(ClassMock1.class.getName()).get(1);
		
		assertNotNull(fld);
		assertEquals("String", fld.getType());
		assertEquals("data", fld.getName());
		assertNull(fld.getInitializer());
		assertEquals(1, fld.getModifiers().size());
		assertEquals("private", fld.getModifiers().get(0));
	}
	
	public void testInitializingFields() throws PreprocessorException {
		FieldDescriptor fld = getFields(ClassMock1.class.getName()).get(0);
		
		assertNotNull(fld);
		assertEquals("long", fld.getType());
		assertEquals("serialVersionUID", fld.getName());
		assertNotNull(fld.getInitializer());
		assertEquals(3, fld.getModifiers().size());
		assertEquals("private", fld.getModifiers().get(0));
		assertEquals("static", fld.getModifiers().get(1));
		assertEquals("final", fld.getModifiers().get(2));
	}
	
	private List<FieldDescriptor> getFields(String className) throws PreprocessorException {
		InputStream cu = getClass().getClassLoader().getResourceAsStream(Name.javaToFs(className));
		try {
			JavaParser parser = JavaParser.create(cu);
			return parser.parse().getTopClasses().get(0).getFields();
		} finally {
			try {
				cu.close();
			} catch(Exception ex) {
			}
		}
	}
}
